<?php

namespace App\Admin\Selectable;

use App\Models\Material;
use App\Models\MaterialClassify;
use App\Models\Supplier;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Selectable;
use Encore\Admin\Grid\Selectable\Radio;
use Encore\Admin\Grid\Selectable\Checkbox;
use Illuminate\Support\Arr;

class Materials extends Selectable
{
    public $model = Material::class;

    public function render($multiple = false)
    {

        $this->make();

        if ($this->imageLayout) {
            $this->setView('admin::grid.image', ['key' => $this->key]);
        } else {
            $this->appendRemoveBtn(true);
        }

        $this->disableFeatures()->paginate($this->perPage)->expandFilter();

        $displayer = $this->multiple ? Checkbox::class : Radio::class;

        $this->prependColumn('__modal_selector__', '<input type="checkbox" id="select-all"/>')->displayUsing($displayer, [$this->key]);

        return $this->grid->render();
    }

    public function make()
    {
    	
        $this->column('name');
        $this->column('id' , '数量')->mystock();
        $this->column('created_at');

        $this->filter(function (Filter $filter) {
            $filter->disableIdFilter();

            $filter->like('name', '名称');
            $filter->equal('material_classify_id', '材料分类')
                ->select(Arr::except(MaterialClassify::selectOptions(), '0'));
            $filter->equal('supplier_id', '供应商')
                ->select(Supplier::all()->pluck('company', 'id'));
        });
    }
}
